package net.maku.framework.beansearcher;

import cn.zhxu.bs.BeanMeta;
import cn.zhxu.bs.IllegalParamException;
import cn.zhxu.bs.ParamFilter;
import cn.zhxu.bs.boot.prop.BeanSearcherParams;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 下标数组参数过滤器, 用于兼容如下形式的前端数组参数：
 * ? key[0]=v1 & key[1]=v2
 * @author Troy.Zhou
 */
@Component
@AllArgsConstructor
public class IndexArrayParamFilter implements ParamFilter {

    static final Pattern KEY_PATTERN = Pattern.compile("([a-zA-Z0-9_]+)\\[([0-9]*)]");

    private final BeanSearcherParams config;

    @Override
    public <T> Map<String, Object> doFilter(BeanMeta<T> beanMeta, Map<String, Object> paraMap) throws IllegalParamException {
        Map<String, Object> newMap = null;
        for (Map.Entry<String, Object> entry : paraMap.entrySet()) {
            String key = entry.getKey();
            Matcher matcher = KEY_PATTERN.matcher(key);
            if (!matcher.matches()) {
                continue;
            }
            if (newMap == null) {
                newMap = new HashMap<>();
            }
            String field = matcher.group(1);
            String index = matcher.group(2);
            newMap.put(field + config.getSeparator() + index, entry.getValue());
        }
        if (newMap != null) {
            paraMap.putAll(newMap);
        }
        return paraMap;
    }

}
